package com.abso.sunlight.api;

import java.util.HashMap;
import java.util.Map;

/**
 * Legislator's title.
 */
public enum Title {

    SENATOR("Senator", "Sen"), REPRESENTATIVE("Representative", "Rep"), DELEGATE("Delegate", "Del"), RESIDENT_COMMISSIONER(
            "Resident Commissioner", "Com"), UNKNOWN("Unknown", "");

    /** The title's name. */
    private String name;

    /** The title's abbreviation. */
    private String abbr;

    /** The map of titles addresses by abbreviations. */
    private static final Map<String, Title> TITLES_BY_ABBR = new HashMap<String, Title>();

    /* static initializer */
    static {
        for (Title title : values()) {
            TITLES_BY_ABBR.put(title.getAbbreviation(), title);
        }
    }

    /**
     * Constructs a new title.
     * 
     * @param name
     *            the title's name.
     * @param abbr
     *            the title's abbreviation.
     */
    Title(String name, String abbr) {
        this.name = name;
        this.abbr = abbr;
    }

    /**
     * Returns the title's abbreviation.
     * 
     * @return the title's abbreviation.
     */
    public String getAbbreviation() {
        return abbr;
    }

    /**
     * Gets the enum constant with the specified abbreviation.
     * 
     * @param abbr
     *            the title's abbreviation.
     * @return the enum constant with the specified abbreviation.
     * @throws SunlightException
     *             if the abbreviation is invalid.
     */
    public static Title valueOfAbbreviation(String abbr) {
        Title title = TITLES_BY_ABBR.get(abbr);
        if (title != null) {
            return title;
        } else {
            return UNKNOWN;
        }
    }

    @Override
    public String toString() {
        return name;
    }

}
